home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 001a / com_and3.zip / LISTER.CMD < prev    next >
OS/2 REXX Batch file  |  1990-02-09  |  8KB  |  307 lines

  1. SET TTHRU OFF                ; Make 1st to allow typeahead
  2. ; ----- List a file, with paging
  3. ;
  4. ;    This script lists a file to the screen, allowing paging back and
  5. ;    forth with PgUp, PgDn.    EOF may be searched, and the EOF search
  6. ;    may be interrupted (by any keypress).
  7. ;
  8. ;    The script's FSAVE stack is limited to 20 positions - so PgUp only
  9. ;    goes back through the last 20 pages viewed.
  10. ; ------------------------------------------------------------------------
  11. ;    This script may be FCALLED (S0 passes the file name)
  12. ; ------------------------------------------------------------------------
  13. ;    R.McG, commenced 9/88
  14. ;           Version 2 to use SCREEN command commenced 2/90
  15. ; ----- Usages -----------------
  16. ;    S0 ------> File name being displayed
  17. ;    S2 ------> Text being searched (if Flag(1) set)
  18. ;    N0 ------> Display size in lines
  19. ;    N1 ------> Display current row #
  20. ;    N2 ------> # non-blank lines on page
  21. ;    N3 ------> Current page #
  22. ;    N4 ------> Read rtn length
  23. ;    N5 ------> Screen attribute
  24. ;    FLAG(0) -> If true, searching EOF
  25. ;    FLAG(1) -> If true, searching for text in S2
  26. ; ------------------------------
  27. ;    Initialization
  28. ;
  29. ;* TRACE ON
  30.    LEGEND "Lister ver 2.0"
  31.    SSIZE N0            ; # lines
  32.    SAVE 0,0 N0,79        ; Save current screen
  33.    CLEAR
  34.    N5 = "_TEXT"                 ; Set text attribute (numeric result)
  35. ;
  36. ;    If file name not passed, query for one.
  37. ;
  38.    IF NOT NULL S0 GOTO Testfile
  39.    S1 = "File to be displayed?"
  40.    GOSUB Ask_File        ; Ask for an output file
  41.    IF NOT SUCCESS GOTO EXIT    ; If ESC'd then stop
  42.    UPPER S0
  43. ;
  44. ;    Test for file name presence
  45. ;
  46. Testfile:
  47.    FOPENI S0 text
  48.    IF NOT SUCCESS
  49.       S0 = S0& " can't be opened"
  50.       GOSUB Error
  51.       GOTO EXIT
  52.       ENDIF
  53. ;
  54. ;    Set-up for display
  55. ;
  56.    N0 = N0-2            ; # lines display
  57.    N3 = 0            ; Set page #
  58.  
  59.    LEGEND S0&": Enter PgUp, PgDn, Home, End, Esc: "
  60.    ON ESCAPE GOSUB Exit
  61.    SET FLAG(0) OFF        ; EOF search
  62.    SET FLAG(1) OFF        ; Text search
  63.    GOTO Page
  64. ;
  65. ;    Terminate script
  66. ;
  67. Exit:
  68.    RESTORE            ; Restore previous screen
  69.    IF FCALLED FRET        ; If called by another
  70.    EXIT             ; else stop
  71. ;
  72. ;    Initialize for a page
  73. ;
  74. Page:
  75.    CLEAR
  76.    N1 = 0            ; Current disp line
  77.    N2 = 0            ; # non-blank lines
  78.    N3 = N3+1            ; Increment page #
  79. ;
  80. ;    Update the legend accordingly
  81. ;
  82.    IF NOT FLAG(0)        ; If not searching EOF
  83.       IF NOT FLAG(1)        ; If not searchin for text
  84.      LEGEND S0&": Enter PgUp, PgDn, Home, End, Esc, F)ind;  Page "*N3
  85.       ELSE
  86.      LEGEND S0&": Searching for text; any keypress to stop;  Page "*N3
  87.      ENDIF
  88.    ELSE
  89.       LEGEND S0&": Searching for EOF; any keypress to stop;  Page "*N3
  90.       ENDIF
  91. ;
  92. ;    Save the position of the start of this page
  93. ;
  94.    FSAVEI
  95.    IF NOT SUCCESS
  96.       FSAVEI SHIFT        ; Save last 20 pos'ns
  97.       FSAVEI
  98.       ENDIF
  99. ;
  100. ;    Display a page's worth
  101. ;
  102. Loop:
  103.      FOR N1 = 0,N0,1
  104.        READ S1 80 N4        ; N4 = # chars read
  105.        IF EOF GOTO EndFile    ; Go no farther
  106.  
  107.        IF NOT NULL S1        ; Skip empty lines
  108.       IF FIND S1 "^L"       ; Top of form
  109.          IF NOT ZERO N2    ; Current page is empty
  110.         GOTO Wait    ; Go no farther
  111.          ELSE
  112.         GOTO Loop    ; Don't display empty pages
  113.         ENDIF
  114.          ENDIF
  115.  
  116.       N2 = N2+1        ; Count non-blank lines
  117.  
  118.       IF NOT FLAG(0)    ; If not searching EOF
  119.          IF NOT FLAG(1)    ; If not searching text
  120.         PRESERVE S1    ; Preserve c/rs etc
  121.         ATSAY N1,0 N5 S1; Display what was read
  122.          ELSE        ; Searching text
  123.         IF FIND S1 S2 GOTO Endfile ; Stop on match; Look for S2 in S1
  124.         IF HITKEY GOTO Hitkey       ; And searching EOF
  125.         ENDIF
  126.       ELSE
  127.          IF HITKEY GOTO Hitkey
  128.          ENDIF
  129.       ENDIF         ; End not null line
  130.        ENDFOR
  131.    GOTO Wait
  132. ;
  133. ;    Keypress during loop
  134. ;
  135. Hitkey:
  136.    KFLUSH            ; Flush the keypress
  137. ;
  138. ;    End of file - reposition to last page, and then wait
  139. ;
  140. EndFile:
  141.    FRESTI            ; Restore position to top of current page
  142.    IF NOT FLAG(0) AND NOT FLAG(1) GOTO Wait ; If not searching for EOF or text
  143. ;
  144. ;    We were searching - display current page
  145. ;
  146.    SET FLAG(0) OFF        ; Done w/ eof search
  147.    SET FLAG(1) OFF        ; Done w/ eof search
  148.    N3 = N3-1            ; Reset Page # for redisplay
  149.    GOTO Page            ; Display last page
  150. ;
  151. ;    End of a page - wait for a keypress
  152. ;
  153. Wait:
  154.    IF FLAG(0) GOTO Page     ; EOF search - no wait
  155.    IF FLAG(1) GOTO Page     ; Text search - no wait
  156. ;
  157. ;    Read the key and interpret
  158. ;
  159.    KEYGET S1
  160.    SWITCH S1
  161.      CASE "4900"                ; Pgup
  162.        GOTO PgUp
  163.        ENDCASE
  164.      CASE "5100"                ; PgDn
  165.        GOTO PgDn
  166.        ENDCASE
  167.      CASE "4700"                ; Home
  168.        GOTO Home
  169.        ENDCASE
  170.      CASE "4F00"                ; End
  171.        GOTO End
  172.        ENDCASE
  173.      CASE "0D"                  ; C/r
  174.        GOTO PgDn
  175.        ENDCASE
  176.      CASE "F"                   ; Find
  177.        GOTO Find
  178.        ENDCASE
  179.      ENDSWITCH
  180.    MESS S1
  181.    SOUND 100,100
  182.    GOTO Wait
  183. ;
  184. ;    Page up (go backwards)
  185. ;
  186. PgUp:
  187.       FRESTOREI         ; Backup current pg
  188.       N3 = N3-1         ; Reset Page # for redisplay
  189.       FRESTOREI         ; Backup one more
  190.       IF NOT SUCCESS
  191.      SOUND 200,100
  192.      GOTO Page
  193.      ENDIF
  194.       N3 = N3-1         ; Reset Page # for redisplay
  195.       GOTO Page
  196. ;
  197. ;    Page down (go forwards)
  198. ;
  199. PgDn:
  200.     GOTO Page
  201. ;
  202. ;    Home (rewind the file)
  203. ;
  204. Home:
  205.     N3 = 0            ; Reset Page # for redisplay
  206.     REWIND
  207.     GOTO Page
  208. ;
  209. ;    End (Search EOF)
  210. ;
  211. End:
  212.    SET FLAG(0) ON        ; Flag searching
  213.    GOTO Page
  214. ;
  215. ;    SUBROUTINE: Find a line
  216. ;    This subroutine modifies S2 and S19
  217. ;
  218. Find:
  219.    S19 = "_ONESCAPE"            ; Save label previously set
  220.    ON ESCAPE GOSUB Find_Esc    ; Escape out of pop-up
  221.    SAVE  20,10 23,70
  222.    BOX     20,10 23,70 (default)
  223.    ATSAY 20,12 (default) " Find subcommand "
  224.    ATSAY 21,12 (default) "Enter the search string (case not signifigant):"
  225. ;
  226. ;    Read a response
  227. ;
  228.    ATGET 22,12 (default) 56 S2    ; Box is 60 wide
  229.    IF NULL S2 GOTO Find_Exit    ; Skip if nothing entered
  230. ;
  231. ;    Set-up for text search
  232. ;
  233.    SET FLAG (1) ON        ; Set searching flag
  234.    ON ESCAPE GOSUB S19        ; Reset original ON ESCAPE label
  235.    RESTORE            ; Clear pop-up
  236.    GOTO Page            ; Start search from this page
  237. ;
  238. ;    Null search string
  239. ;
  240. Find_Exit:
  241.    ON ESCAPE GOSUB S19        ; Reset original ON ESCAPE label
  242.    RESTORE            ; Clear pop-up
  243.    GOTO Wait            ; Wait for another key
  244. ;
  245. ;    Subroutine: Escape entered, or null search string
  246. ;    .. This subroutine is entered upon ESCAPE at the ATGET instruction
  247. ;    .. above.  Upon return from this subroutine, the instruction after
  248. ;    .. ATGET will be executed.  The string S2 is set to a null value to
  249. ;    .. imitate no entry, and therefore abort of the pop-up.
  250. ;
  251. Find_Esc:
  252.    S2 = ""                      ; Make a null string
  253.    RETURN            ; Return to instruction after ATGET
  254. ;
  255. ; ----- Subroutine: Ask for a script file name
  256. ;    S1 passes the prompt used
  257. ;    SUCCESS returns fact script file successfully opened
  258. ;    S0 returns the fname
  259. ;
  260. Ask_File:
  261.    WOPEN 10 10 13 70 (default) ASK_ESC
  262.    ATSAY 10 12 (default) " Lister Fname "
  263.    ATSAY 11 12 (default) S1(0:56)
  264.    ATSAY 13 30 (default) " Press ESC to cancel "
  265.  
  266.    ATGET 12 12 (default) 54 S0; Read new fname
  267.    WCLOSE
  268.    ;
  269.    ;       Attempt to execute the entry
  270.    ;
  271.    LJ S0            ; Left justify
  272.    S0 = S0 &""                  ; Trim spaces
  273.    UPPER S0            ; Make pretty
  274.    IF NOT NULL S0        ; If nothing entered
  275.       SET SUCCESS ON
  276.    ELSE
  277.       SET SUCCESS OFF
  278.       ENDIF
  279.    RETURN            ; And done
  280. ;
  281. ; ----- Escape during a subwindow
  282. ;    .. S0 is returned null
  283. ;
  284. Ask_Esc:
  285.    S0 = ""                      ; Make a null return
  286.    RETURN
  287. ;
  288. ; ----- Subroutine: Fatal error.  Open a window, and display a message
  289. ;    S0 passes the error message(s)
  290. ;
  291. Error:
  292.    WOPEN 10,10,12,70 (default) Err_Esc
  293.    ATSAY 10,12 (default) " Lister Error "
  294.    ATSAY 11,12 (default) S0(0:55); Max msg width 55 chars
  295.    ATSAY 12,26 (default) " Press any key to continue "
  296.    ;
  297.    ;    Wait a keypress
  298.    ;
  299.    KEYGET S0               ; Wait for any key
  300.    WCLOSE
  301.    RETURN
  302.    ;
  303.    ;    Escape during this screen
  304.    ;
  305. Err_Esc:
  306.    RETURN
  307.